<?php

/**
 * Page of a single product and navigation between
 * similiar product pages
 *
 * @author Apple
 */

class productPageActions extends myFrontendAction
{
	public function preExecute()
    {
		parent::preExecute();

        $object = $this->getRoute()->getProductObject();

        $this->project = $object['product'];
        $this->slide   = $object['slide'];

		// Redirect if slide is a plain string
		if(is_string($this->slide)) {
			$this->redirect($this->slide . '?page=' . $this->getUser()->getAttribute('page', 1));
		}
	}


	/**
     * Project page
     */
	public function executeIndex(sfWebRequest $request)
    {
        if($request->isXmlHttpRequest()) {
            $this->setLayout (false);
        } else $this->setLayout ('products');
        
        /**
         * Variables in the template
         */
        $location = null;                 // Link to the next or prev product (depends on request)
        $slide    = array();              // CURRENT project slide (with offset)
        $project  = $this->project;       // Current project information (dump, if needed)

        
        /**
         * We would like to give users possibility to adventure between our projects
         * This pice of code makes them happy
         */
        if($request->getParameter('where')) {
            $projectCarousel = Doctrine::getTable('Product')->createQuery('p')
                ->innerJoin('p.Translation pt')->where('pt.lang = ?', array($this->getUser()->getCulture()));
            
            if($request->getParameter('where') == 'prev') {
                   $projectCarousel = $projectCarousel
                    ->andWhere('p.id < ?', array($project->getId()))
                    ->execute()
                    ->getLast();
            } else {
                $projectCarousel = $projectCarousel
                    ->where('p.id > ?', array($project->getId()))
                    ->execute()
                    ->getFirst();
            }

            // What the ... ?
            // Is it first or last project?
            if(!$projectCarousel) {
                $projectCarousel = Doctrine_Query::create();
                
                if($request->getParameter('where') == 'prev') {
                    // Return me the last project, Doctrine, please =(
                    $projectCarousel = $projectCarousel->select('max(id)');
                } else  $projectCarousel = $projectCarousel->select('min(id)');

                $projectCarousel = $projectCarousel->from('Product')->execute()->getFirst();

                /**
                 * Now we select that product
                 */
                if($projectCarousel) {
                    // Gold identifier
                    $id = $request->getParameter('where') == 'prev' ? $projectCarousel->getMax() : $projectCarousel->getMin();

                    // Next product (cycle)
                    $suitableProject = Doctrine::getTable('Product')->createQuery('p')
                        ->innerJoin('p.Translation pt')
                        ->where('pt.id = ?', $id)
                        ->andWhere('pt.lang = ?', array($this->getUser()->getCulture()))
                        ->execute();

                    // We need to only know this link
                    if($suitableProject)
                        $location = LinkGen::getInstance(LinkGen::PRODUCT)->collectionLinks($suitableProject);



                    if($request->isXmlHttpRequest()) {
                        exit($location[$id]); // That's only we need. No shit in dev environment
                    } else {
                        $this->redirectIf($location, $location[$id]);
                    }
                }

            } else {
                $location = str_replace('#nolink', '',
                    LinkGen::getInstance(LinkGen::PRODUCT)->link($projectCarousel->getId())
                );
                
                if($request->isXmlHttpRequest()) {
                    exit($location);
                } else {
                    $this->redirectIf($location, $location);
                }
            }
        }
	}
};